package aod.android.com.bernay.mine.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.loginandregister.bean.PhoneCodeBean;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/3 20:53
 * @Email jamesKing0812@gmail.com
 */
public class PasswordActivity extends BernayBaseActivity implements View.OnClickListener {
    @HiView(R.id.tv_callPhone)
    TextView mCallPhone;

    @HiView(R.id.et_messageCode)
    EditText messageCode;

    @HiView(R.id.tv_getMessage)
    TextView getMessageCode;

    private String loginName;
    private TimeCount time;
    private PhoneCodeBean msgCodeBean;
    private Bundle extras;

    @Override
    protected int initLayout() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initData() {
        extras = getIntent().getExtras();
        time = new TimeCount(60000, 1000);
        loginName = extras.getString("loginName");
        mCallPhone.setText(loginName);
        initTitle(getString(R.string.change_password), getString(R.string.add_fixing_next), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                String message = messageCode.getText().toString();
                if (!"".equals(message)){
                    if(msgCodeBean.getData().get(0).getCheckCode().equals(message)){
                        bundle.putString("msgCode",message);
                        bundle.putString("loginName",loginName);
                        goActivity(SetNewPasswordActivity.class,bundle);
                    }else{
                        showToast("验证码输入错误，请重新输入！");
                    }
                }else{
                    showToast("请输入短信验证码");
                }
            }
        });
        getMessageCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_getMessage:
                getMessageCode();
                break;
            default:
                break;
        }
    }

    /**
     * 获取短信验证码
     */
    public void getMessageCode(){
        BernayApi.http().getPhoneCode(loginName, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                msgCodeBean = gson.fromJson(json,PhoneCodeBean.class);
                if(msgCodeBean.getStatus()==1){
                    showToast("验证码发送成功");
                    time.start();
                }else{
                    showToast(msgCodeBean.getMsg());
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getMessageCode.setText("("+millisUntilFinished / 1000 +") 秒后可重新发送");
        }

        @Override
        public void onFinish() {
            getMessageCode.setText("重新获取验证码");
            getMessageCode.setClickable(true);
        }
    }
}
